package com.sunfeng.chap1;


import java.nio.CharBuffer;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2021/10/8  10:26
 */
public class Test11_1 {
    public static void main(String[] args) {
        CharBuffer charBuffer = CharBuffer.allocate(20);
        System.out.println ("A "+charBuffer.position()+" "+charBuffer.limit());
        charBuffer.put("我是中国人我在中华人民共和国");
        System.out.println ("B "+charBuffer.position()+" "+charBuffer.limit());
        charBuffer.position(0);
        System.out.println ("C "+charBuffer.position()+" "+charBuffer.limit());

        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.println(charBuffer.get());
        }
        System.out.println();
        //上面的代码是错误的

        //下面的代码是正确读取代码
        System.out.println ("D "+charBuffer.position()+" "+charBuffer.limit());

        charBuffer.clear();
        System.out.println ("E "+charBuffer.position()+" "+charBuffer.limit());

        charBuffer.put("我是山东人");
        System.out.println ("F "+charBuffer.position()+" "+charBuffer.limit());

        charBuffer.limit(charBuffer.position());
        charBuffer.position(0);

        System.out.println("G position"+charBuffer.position()+ charBuffer.limit());

        charBuffer.rewind();
        charBuffer.clear();
        charBuffer.flip();
        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.println(charBuffer.get());
        }


    }
}
